﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using xTeam.HengLe.DataAccess;
using System.ComponentModel;
using xTeam.HengLe.Entity;
using System.Web.Security;

namespace xTeam.HengLe.Controller.Manage
{
    public class HomeController : BaseController
    {
        public HomeController()
        {
            // 数据库连接字符串（BaseController获得）
            AdminDataAccess.Con = _connectionString;
        }

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">登录名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login([DefaultValue("")]string userName, [DefaultValue("")]string password)
        {
            string msg = string.Empty;

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                msg = "登录名称或密码不能为空！";
            }
            else
            {
                List<EAdmin> adminList = AdminDataAccess.GetByAdminName(userName);

                if (adminList.Count == 0)
                {
                    msg="登录名称出错！";
                }
                else if (password == adminList[0].Password)
                {
                    msg = "location.replace('"+Url.Action("Index","Home")+"');";
                    FormsAuthentication.SetAuthCookie(userName, true);
                    Session["admin"] = userName;
                    return Content(msg);
                }

                msg ="登录密码出错！";
            }

            return Content(string.Format("alert('{0}');", msg));
        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return Redirect("/");
        }
    }
}
